﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WinformMDB
{
    public partial class Form1 : Form
    {
        static string path = "D:\\Code\\测井数据库_套管节箍深度表.mdb";
        static string tablename = "AJCJ12";
        static string row = "top 107";
        static string sqlComm = "Select " + row + " * From " + tablename;
        static int rowNumPerPage = 10;
        string sqlComm1;
        //----暂时不需要
        int rowNum = 0;
        string colName = "";
        //----查询及页码相关
        int startRowNum = 0;
        int endRowNum = 0;
        int maxPageNum;
        int currentPageNum;
        //----类
        MDBConn mDBConn = new MDBConn(path);
        DataSet dataPool = new DataSet();
        DataTable da = new DataTable();
        DataTable datb = new DataTable();
        OleDbCommand command = new OleDbCommand();

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            //调整单元格大小
            this.dataGridView1.AutoResizeColumns();
            this.dataGridView1.AutoResizeRows();
            this.dataGridView1.AutoResizeColumnHeadersHeight();
        }

        private void ReadFile_Click_1(object sender, EventArgs e)
        {
            //初始化页面数据
            this.endRowNum = rowNumPerPage - 1;
            this.currentPageNum = 1;
            this.maxPageNum = (this.da.Rows.Count / rowNumPerPage) + 1;
            int temp = startRowNum + endRowNum
            //sqlComm1 = "Select top " + rowNumPerPage + " * From "+ tablename +" Where (Select top "+ +" From AJCJ12 Order By JHDM Desc;)";

            //读取文件并显示
            try
            {
                mDBConn.Open();
                command = mDBConn.WriteInSQL(sqlComm);
                //this.da = mDBConn.GetDataSetByAdapter(sqlComm);
                this.da = mDBConn.GetDataSetByReader(command);
                mDBConn.Close();
            }
            catch
            {
                bool success = false;
                //mDBConn = null;
            }
            
            datb = CopyRows(da, datb, this.startRowNum, this.endRowNum);

            //显示数据,路径,页码。
            this.dataGridView1.DataSource = datb;
            this.FilePath.Text = path;
            this.textBox1.Text = sqlComm;
            this.curPage.Text = currentPageNum.ToString() + " / " + maxPageNum.ToString();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {/*
            //查询指定单元格-未完成
            command = mDBConn.WriteInSQL(sqlComm);
            string check = (string)mDBConn.GetValue(command, this.rowNum, this.colName);
        */}

        private void LastPage_Click(object sender, EventArgs e)
        {
            //上一页
            if (this.currentPageNum <= 1)
                WarnPageOperation(false);
            else
            {
                NormalPageOperation(false);
            }


        }

        private void NextPage_Click(object sender, EventArgs e)
        {
            //下一页
            if (this.currentPageNum >= this.maxPageNum)
                WarnPageOperation(true);
            else if (this.da.Rows.Count - endRowNum < rowNumPerPage)
            {
                //限制到上限，不能超。
                endRowNum = this.da.Rows.Count - 1;
                this.startRowNum += rowNumPerPage;
                this.currentPageNum += 1;
                datb = CopyRows(da, datb, this.startRowNum, this.endRowNum);
                this.dataGridView1.DataSource = datb;
                this.FilePath.Text = path;
                this.curPage.Text = currentPageNum.ToString() + " / " + maxPageNum.ToString();
                endRowNum = startRowNum + rowNumPerPage;
            }
            else
            {
                //this.endRowNum = 
                NormalPageOperation(true);
            }

        }

        public DataTable CopyRows(DataTable dBig, DataTable dSmall, int start, int end)
        {
            //截取几行
            dSmall = dBig.Clone();
            for (int i = start; i <= end; i++)
            {
                dSmall.ImportRow(dBig.Rows[i]);
            }

            return dSmall;
        }

        public void NormalPageOperation(bool lastORnext)
        {
            if (lastORnext)//next TRUE last FALSE
            {
                this.startRowNum += rowNumPerPage;
                this.endRowNum += rowNumPerPage;
                this.currentPageNum += 1;
                datb = CopyRows(da, datb, this.startRowNum, this.endRowNum);
                this.dataGridView1.DataSource = datb;
                this.FilePath.Text = path;
                this.curPage.Text = currentPageNum.ToString() + " / " + maxPageNum.ToString();
            }
            else
            {
                this.startRowNum -= rowNumPerPage;
                this.endRowNum -= rowNumPerPage;
                this.currentPageNum -= 1;
                datb = CopyRows(da, datb, this.startRowNum, this.endRowNum);
                this.dataGridView1.DataSource = datb;
                this.FilePath.Text = path;
                this.curPage.Text = currentPageNum.ToString() + " / " + maxPageNum.ToString();
            }
        }

        public void WarnPageOperation(bool LastOrNext)
        {
            if (LastOrNext)
            {
                string warring = "无法向后翻页。";
                this.FilePath.Text = warring;
            }
            else
            {
                string warring = "无法向前翻页。";
                this.FilePath.Text = warring;
            }
        }
    }
}
